package cn.dzhang.spring.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.validation.constraints.Pattern;
import javax.xml.bind.annotation.XmlRootElement;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

@Table(name = "authorities")
@Entity
@XmlRootElement
public class Authority implements Serializable {

	private static final long serialVersionUID = -3941299600529064739L;
	@Id
	@GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
	@GeneratedValue(generator = "uuid")
	private String id;
	@NotBlank
	@Length(max = 15, min = 5)
	@Column(length = 15)
	@Pattern(regexp = "^ROLE_\\w+")
	private String name;
	@Column
	private String description;
	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "authorities")
	private List<User> users = new ArrayList<User>();

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	@Override
	public String toString() {
		return "Authority [id=" + id + ", name=" + name + ", description="
				+ description + "]";
	}
}
